//
//  BChargeCashCashContentView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/7.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BChargeCashCashContentView.h"

//
#import "BCTitleTextFieldView.h"
#import "BCPutCoinNumbersView.h"
//

static CGFloat BChargeCashCashContentViewAddressNumberPadding = 15.0;
static CGFloat BChargeCashCashContentViewNumberBottomPadding = 5.0;
static CGFloat BChargeCashCashContentViewFeeCaptionPadding = 20.0;

@interface BChargeCashCashContentView () <BCPutCoinNumbersViewDelegate>

@property (nonatomic, strong) UIScrollView *contentScroll;

/**
 *BCTitleTextFieldView
 */
@property (nonatomic, strong) BCTitleTextFieldView *putAddressView;

/**
 *BCTitleTextFieldView
 */
@property (nonatomic, strong) BCPutCoinNumbersView *putNumbersView;
/**
 *available
 */
@property (nonatomic, strong) UILabel *availableLabel;


/**
 *BCTitleTextFieldView
 */
@property (nonatomic, strong) BCTitleTextFieldView *putFeeView;

/**
 *captionLabel
 */
@property (nonatomic, strong) QMUILabel *captionLabel;

@end

@implementation BChargeCashCashContentView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    self.amountView = [BChargeCashCashArrivelAmountView new];
    [self addSubview:self.amountView];
    [self.amountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.width.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(75.0);
    }];
    self.amountView.amount = @"--";
    //
    self.cashButton = [QMUIButton new];
    [self.cashButton setTitleColor:[UIColor qmui_colorWithHexString:@"#9B9B9B"] forState:UIControlStateNormal];
    self.cashButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    [self.amountView addSubview:self.cashButton];
    [self.cashButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.amountView).offset(-20.0);
        make.centerY.mas_equalTo(self.amountView.amountLabel);
        make.width.mas_equalTo(105);
        make.height.mas_equalTo(35.0);
    }];
    self.cashButton.layer.cornerRadius = 4;
    self.cashButton.layer.borderColor = [UIColor qmui_colorWithHexString:@"#979797"].CGColor;
    self.cashButton.layer.borderWidth = 1;
    
    self.contentScroll = [UIScrollView new];
    self.contentScroll.alwaysBounceVertical = true;
    self.contentScroll.showsVerticalScrollIndicator = false;
    [self addSubview:self.contentScroll];
    [self.contentScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.amountView.mas_top);
    }];
    //
    [self setContents];
}
- (void)setContents {
    CGFloat defaultHeight = [BCTitleTextFieldView defaultHeight];
    self.putAddressView = [BCTitleTextFieldView new];
    [self.contentScroll addSubview:self.putAddressView];
    [self.putAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentScroll);
        make.top.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll);
        make.height.mas_equalTo(defaultHeight);
    }];
    //添加二维码按钮
    self.qrCodeButton = [QMUIButton new];
    [self.qrCodeButton setImage:LOAD_IMAGE(@"wallet_cash_qr_code") forState:UIControlStateNormal];
    [self.putAddressView addSubview:self.qrCodeButton];
    self.qrCodeButton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.qrCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.putAddressView);
        make.right.mas_lessThanOrEqualTo(self.putAddressView);
    }];
    //
    self.putNumbersView = [BCPutCoinNumbersView new];
    self.putNumbersView.delegate = self;
    [self.contentScroll addSubview:self.putNumbersView];
    [self.putNumbersView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll);
        make.top.mas_equalTo(self.putAddressView.mas_bottom).offset(BChargeCashCashContentViewAddressNumberPadding);
        make.height.mas_equalTo(defaultHeight);
    }];
    //
    self.availableLabel = [UILabel new];
    self.availableLabel.numberOfLines = 0;
    self.availableLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.availableLabel.font = [UIFont systemFontOfSize:11.0 weight:UIFontWeightRegular];
    [self.contentScroll addSubview:self.availableLabel];
    [self.availableLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.putNumbersView.mas_bottom).offset(BChargeCashCashContentViewNumberBottomPadding);
        make.left.mas_equalTo(self.contentScroll).offset(20.0);
        make.width.mas_equalTo(self.contentScroll).offset(-40.0);
    }];
    //
    self.putFeeView = [BCTitleTextFieldView new];
    self.putFeeView.textField.enabled = false;
    [self.contentScroll addSubview:self.putFeeView];
    [self.putFeeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll);
        make.top.mas_equalTo(self.availableLabel.mas_bottom).offset(BChargeCashCashContentViewAddressNumberPadding);
        make.height.mas_equalTo(defaultHeight);
    }];
    //
    self.captionLabel = [QMUILabel new];
    self.captionLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.captionLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    self.captionLabel.numberOfLines = 0;
    [self.contentScroll addSubview:self.captionLabel];
    [self.captionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.putFeeView.mas_bottom).offset(BChargeCashCashContentViewFeeCaptionPadding);
        make.left.mas_equalTo(self.contentScroll).offset(20.0);
        make.width.mas_equalTo(self.contentScroll).offset(-40.0);
    }];
}



- (void)setCashData:(BCUserWalletCashData *)cashData {
    _cashData = cashData;
    //
    if (cashData) {
        self.putNumbersView.digits = MAX(cashData.data.scaler.length-1, 0);
        //
        self.putAddressView.title = cashData.flow.addr;
        self.putAddressView.placeholder = cashData.flow.addr_placeholder;
        //
        self.putNumbersView.title = cashData.flow.amount;
        NSString *amount_placeholderReplacing = [cashData.data.min_cash stringByAppendingString:@""];
        self.putNumbersView.placeholder = [cashData.flow.amount_placeholder stringByReplacingOccurrencesOfString:@"{}" withString:amount_placeholderReplacing];
        //
        NSString *available = [cashData.flow.available stringByAppendingString:[cashData.data.balance stringByAppendingString:cashData.data.acronyms]];
        self.availableLabel.text = available;
        //        [self.availableLabel sizeToFit];
        //
        self.putFeeView.title = cashData.flow.handling;
        self.putFeeView.placeholder = cashData.flow.handling;
        //
        if (cashData.data.handling_type == BCUserWalletCashDataHandlingType_1) {//固定金额
            self.putFeeView.textField.text = cashData.data.handling;
        }else if (cashData.data.handling_type == BCUserWalletCashDataHandlingType_2) {//百分比
            CGFloat handling = cashData.data.handling.floatValue;
            CGFloat handling100 = handling*100;
            self.putFeeView.textField.text = [NSString stringWithFormat:@"%.2f%%",handling100];
        }else {//固定比例
            self.putFeeView.textField.text = cashData.data.handling;
        }
        //
        NSString *promptReplacing = [cashData.data.min_cash stringByAppendingString:cashData.data.acronyms];
        self.captionLabel.text = [cashData.flow.prompt stringByReplacingOccurrencesOfString:@"{}" withString:promptReplacing];
        //
        [self.captionLabel sizeToFit];
        [self layoutIfNeeded];
        //
        CGFloat contentHeight = 0;
        CGFloat defaultHeight = [BCTitleTextFieldView defaultHeight];
        contentHeight += defaultHeight;
        contentHeight += BChargeCashCashContentViewAddressNumberPadding;
        contentHeight += defaultHeight;
        contentHeight += BChargeCashCashContentViewNumberBottomPadding;
        contentHeight += CGRectGetHeight(self.availableLabel.frame);
        contentHeight += BChargeCashCashContentViewAddressNumberPadding;
        contentHeight += defaultHeight;
        contentHeight += BChargeCashCashContentViewFeeCaptionPadding;
        contentHeight += CGRectGetHeight(self.captionLabel.frame);
        //
        self.contentScroll.contentSize = CGSizeMake(0, contentHeight);
        
        //
        self.amountView.title = cashData.flow.arrival;
        [self.cashButton setTitle:cashData.flow.submit forState:UIControlStateNormal];
    }
}


#pragma mark - BCPutCoinNumbersViewDelegate
- (void)putCoinNumbersViewTextDidChanged:(NSString *)text {
    if (text.length) {
        //余额
        CGFloat balance = self.cashData.data.balance.floatValue;
        CGFloat inputNumber = text.floatValue;
        if (inputNumber>balance) {
            self.amountView.amount = nil;
        }else {
            CGFloat number = 0;
            //扣除的
            CGFloat handling = self.cashData.data.handling.floatValue;
            if (self.cashData.data.handling_type==BCUserWalletCashDataHandlingType_2) {//百分比
                number = inputNumber*(1-handling);
            }else {//固定扣除
                number = inputNumber-handling;
            }
            //确认小数点位数
            NSInteger digits = MAX(self.cashData.data.scaler.length-1, 0);
            //
            self.amountView.amount = [self number:number digits:digits];
        }
    }else {
        self.amountView.amount = nil;
    }
}
- (NSString *)number:(CGFloat)number digits:(NSInteger)digits {
    switch (digits) {
        case 0:
            return [NSString stringWithFormat:@"%.0f",number];
            break;
        case 1:
            return [NSString stringWithFormat:@"%.1f",number];
            break;
        case 2:
            return [NSString stringWithFormat:@"%.2f",number];
            break;
        case 3:
            return [NSString stringWithFormat:@"%.3f",number];
            break;
        case 4:
            return [NSString stringWithFormat:@"%.4f",number];
            break;
        case 5:
            return [NSString stringWithFormat:@"%.5f",number];
            break;
        case 6:
            return [NSString stringWithFormat:@"%.6f",number];
            break;
        case 7:
            return [NSString stringWithFormat:@"%.7f",number];
            break;
        case 8:
            return [NSString stringWithFormat:@"%.8f",number];
            break;
        case 9:
            return [NSString stringWithFormat:@"%.9f",number];
            break;
        default:
            return [NSString stringWithFormat:@"%.10f",number];
            break;
    }
}




- (void)clearInputData {
    self.putAddressView.textField.text = nil;
    self.putNumbersView.textField.text = nil;
}


- (void)showContent:(BOOL)show {
    self.contentScroll.alpha = show?1:0;
    self.amountView.alpha = self.contentScroll.alpha;
    [self clearInputData];
}


- (NSString *)address {
    NSString *text = self.putAddressView.textField.text;
    if (text.length==0) {
        [self.putAddressView.textField becomeFirstResponder];
    }
    return text;
}
- (NSString *)number {
    NSString *text = self.putNumbersView.textField.text;
    if (text.length==0) {
        [self.putNumbersView.textField becomeFirstResponder];
    }
    return text;
}

/**
 设置提币地址
 */
- (void)setCoinAddress:(NSString *)address {
    self.putAddressView.textField.text = address;
}



@end
